package cn.gd.my.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class PageFilter
 */
@WebFilter("/PageFilter")
public class PageFilter implements Filter {
	
	String subffix;
	
	@Override
	public void destroy() {
		System.out.println("Invoke Filter Destroy() method...");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
		System.out.println("Invoke Filter doFilter() method...");
		HttpServletRequest req2 = (HttpServletRequest)req;
		System.out.println("收到请求："+req2.getRequestURL());
		String reqSubffix = req2.getRequestURI();
		if(reqSubffix.equals(subffix)){
			System.out.println("请求通过拦截："+req2.getRequestURL());
			chain.doFilter(req, res);
		}
		else
			System.out.println("已拦截该非法请求："+req2.getRequestURL());
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		System.out.println("Invoke Filter init() method...");
		subffix = config.getInitParameter("firstFilter");
	}

}
